﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using Red.Controls.ViewModels;
using Red.EngineData.View;

namespace Red.Controls
{
    /// <summary>
    /// Interaction logic for TransformControl.xaml
    /// </summary>
    public partial class TransformControl : UserControl
    {
        #region data

        private const UInt32    _translateButtonFlags = 1 << 0;
        private const UInt32    _rotateButtonFlags    = 1 << 1;
        private const UInt32    _scaleButtonFlags     = 1 << 2;

        #endregion

        #region c-tor

        public TransformControl()
        {
            InitializeComponent();
        }

        #endregion

        #region event handlers

        private async void TranslateCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                await ( (DataItem)DataContext ).SetSubValueAsync( "gizmo.type", 0 );
                RefreshButtons( _translateButtonFlags );
            }
        }

        private async void RotateCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                await ( (DataItem)DataContext ).SetSubValueAsync( "gizmo.type", 1 );
                RefreshButtons( _translateButtonFlags );
            }
            RefreshButtons(_rotateButtonFlags);
        }

        private async void ScaleCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                await ( (DataItem)DataContext ).SetSubValueAsync( "gizmo.type", 2 );
                RefreshButtons( _translateButtonFlags );
            }
            RefreshButtons(_scaleButtonFlags);
        }

        private void TransformCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private async void ChangeSpaceCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                await ( (DataItem)DataContext ).SetSubValueAsync( "gizmo.space", _changeSpaceButton.IsChecked.Value ? 1 : 0 );
                RefreshButtons( _translateButtonFlags );
            }
        }

        private async void CycleGizmosCommand_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                UInt32 gizmoID = await( (DataItem)DataContext ).GetSubValueAsync<UInt32>( "gizmo.type" );

                gizmoID = ++gizmoID % 3;

                int flags = ( gizmoID == 0u ) ? 1 << 0 :
                                    ( gizmoID == 1u ) ? 1 << 1 : 1 << 2;

                RefreshButtons( (UInt32)flags );
                await ( (DataItem)DataContext ).SetSubValueAsync( "gizmo.type", gizmoID );
            }
        }

        #endregion

        #region private methods

        private void RefreshButtons(UInt32 flags)
        {
            _translationButton.IsChecked = ((flags & _translateButtonFlags) == _translateButtonFlags);
            _rotationButton.IsChecked    = ((flags & _rotateButtonFlags   ) == _rotateButtonFlags   );
            _scaleButton.IsChecked       = ((flags & _scaleButtonFlags    ) == _scaleButtonFlags    );
        }

        #endregion
    }
}
